/**
 * @class 文本标签组构建器
 * @author DuskyHuang 圣子
 * @description 为不同类型的3D物体体统2D标签说明，文本会被优化和视野裁剪。
*/

import { _decorator, Node, Prefab, UITransform } from 'cc';
import { Mediator, utils } from 'cfw';
import { setLayer } from 'helpers';
import { LAYER_UI_2D, LAYER_UI_BG } from "const";
import { CelestialLabel } from 'prefabs';
const { findNode } = utils;

const { ccclass, property } = _decorator;

@ccclass('cocosmos.hud-builder')
export class HudBuilder extends Mediator {

    @property(Prefab) CelLabel: Prefab = null;

    protected onLoad(): void {
        CelestialLabel.template = this.CelLabel;
        this._buildLayers();
    }

    private _buildLayers(): void {
        this._addLayer('coord', false)
            ._addLayer('const', false)
            ._addLayer('stars', false)
            ._addLayer('planetary', true)
            ._addLayer('solar', true)
            ._addLayer('cluster', true)
            ._addLayer('nebula', true)
            ._addLayer('milky', true)
            ._addLayer('galaxy', true)
            ._addLayer('ruler', true)
            ._addLayer('struct', true);
    }

    private _addLayer(style: style, front: boolean): this {
        const layer = new Node(`${style.charAt(0).toUpperCase() + style.substring(1)} Layer`);
        layer.addComponent(UITransform);
        CelestialLabel.Containers[style] = setLayer(layer, front ? LAYER_UI_2D : LAYER_UI_BG);
        const affix = front ? 'Front' : 'Back';
        layer.parent = findNode(`${affix} HUD`, `Canvas ${affix}`);
        return this;
    }
}